package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemDeploy;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemPartner;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMain;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.*;
import com.glsc.ngateway.platform.service.glres.GlscSystemDeployService;
import com.glsc.ngateway.platform.service.glres.GlscSystemPartnerService;
import com.glsc.ngateway.platform.service.glres.GlscSystemResService;
import com.glsc.ngateway.platform.service.system.SystemMainService;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * 资源管理-系统资源拓扑图
 */
@RestController
@RequestMapping("/api/res/topological")
@Slf4j
public class GlscTopologicalGraphController {

    @Resource
    private UserCache userCache;

    @Resource
    private SystemMainService systemMainService;

    @Resource
    private GlscSystemResService glscSystemResService;

    @Resource
    private GlscSystemPartnerService glscSystemPartnerService;

    @Resource
    private GlscSystemDeployService glscSystemDeployService;


    /**
     * 根据系统id和类型生成拓扑图
     *
     * @param
     * @return
     */
    @GetMapping("/system/graph")
    public PlatformResponse<TopologicalGraphDto> systemGraph(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        HashSet<String> nameSet = new HashSet<>();

        TopologicalGraphDto graph = initGraph();
        SystemMain system = systemMainService.findSimpleInfoById(id);
        TopologicalGraphNodeDto mainNode = TopologicalGraphNodeDto.builder().build();
        mainNode.setId(String.valueOf(system.getOpId()));
        mainNode.setName(system.getOpName());
        mainNode.setSymbolSize(60);
        mainNode.setCategory(0);
        mainNode.setIps(getIpList(system.getOpId(), "1"));
        nameSet.add(mainNode.getName());

        List<TopologicalGraphCategoryDto> categories = graph.getCategories();
        categories.add(TopologicalGraphCategoryDto.builder().name("本系统").build());
        categories.add(TopologicalGraphCategoryDto.builder().name("关联系统").build());
        //categories.add(TopologicalGraphCategoryDto.builder().name("被接入系统").build());
        categories.add(TopologicalGraphCategoryDto.builder().name("关联组件").build());

        List<TopologicalGraphNodeDto> nodes = graph.getNodes();
        List<TopologicalGraphLinkDto> links = graph.getLinks();
        nodes.add(mainNode);

        //接入系统
        //{"pageNo":1,"pageSize":10,"systemId":"331","partnerType":"in_system","sourceType":"1"}
        GlscSystemPartnerDto jieRuDto = GlscSystemPartnerDto.builder().systemId(id).partnerType("in_system").sourceType("1").build();
        Page<GlscSystemPartner> jieRuPage = glscSystemPartnerService.page(jieRuDto);
        if (CollectionUtils.isNotEmpty(jieRuPage.getContent())) {

            for (GlscSystemPartner partner : jieRuPage.getContent()) {
                if (nameSet.contains(partner.getPartnerName())) {
                    continue;
                }

                TopologicalGraphNodeDto node = TopologicalGraphNodeDto.builder().build();
                TopologicalGraphLinkDto link = TopologicalGraphLinkDto.builder().source(String.valueOf(partner.getSystemId())).target(String.valueOf(partner.getPartnerSystemId())).build();

                node.setId(String.valueOf(partner.getPartnerSystemId()));
                node.setName(partner.getPartnerName());
                node.setSymbolSize(30);
                node.setIps(getIpList(partner.getPartnerSystemId(), partner.getPartnerSourceType()));
                node.setCategory(1);

                nameSet.add(node.getName());
                nodes.add(node);
                links.add(link);
            }
        }

        //被接入系统
        //{"pageNo":1,"pageSize":10,"systemId":"331","partnerType":"out_system","sourceType":"1"}
        /*GlscSystemPartnerDto beiJieRuDto = GlscSystemPartnerDto.builder().systemId(id).partnerType("out_system").sourceType("1").build();
        Page<GlscSystemPartner> beiJieRuPage = glscSystemPartnerService.page(beiJieRuDto);
        if (CollectionUtils.isNotEmpty(beiJieRuPage.getContent())) {

            for (GlscSystemPartner partner : beiJieRuPage.getContent()) {
                if (nameSet.contains(partner.getPartnerName())) {
                    continue;
                }

                TopologicalGraphNodeDto node = TopologicalGraphNodeDto.builder().build();
                TopologicalGraphLinkDto link = TopologicalGraphLinkDto.builder().source(String.valueOf(partner.getSystemId())).target(String.valueOf(partner.getPartnerSystemId())).build();

                node.setId(String.valueOf(partner.getPartnerSystemId()));
                node.setName(partner.getPartnerName());
                node.setSymbolSize(30);
                node.setCategory(1);
                node.setIps(getIpList(partner.getPartnerSystemId(), partner.getPartnerSourceType()));

                nameSet.add(node.getName());
                nodes.add(node);
                links.add(link);
            }
        }*/

        //接入组件
        //{"pageNo":1,"pageSize":10,"systemId":"331","partnerType":"in_middleware","sourceType":"1"}
        GlscSystemPartnerDto zuJianDto = GlscSystemPartnerDto.builder().systemId(id).partnerType("in_middleware").sourceType("1").build();
        Page<GlscSystemPartner> zuJianPage = glscSystemPartnerService.page(zuJianDto);
        if (CollectionUtils.isNotEmpty(zuJianPage.getContent())) {

            for (GlscSystemPartner partner : zuJianPage.getContent()) {
                if (nameSet.contains(partner.getPartnerName())) {
                    continue;
                }

                TopologicalGraphNodeDto node = TopologicalGraphNodeDto.builder().build();
                TopologicalGraphLinkDto link = TopologicalGraphLinkDto.builder().source(String.valueOf(partner.getSystemId())).target("z" + String.valueOf(partner.getPartnerSystemId())).build();

                node.setId("z" + String.valueOf(partner.getPartnerSystemId()));
                node.setName(partner.getPartnerName());
                node.setSymbolSize(20);
                node.setCategory(2);
                node.setIps(getIpList(partner.getPartnerSystemId(), partner.getPartnerSourceType()));

                nameSet.add(node.getName());
                nodes.add(node);
                links.add(link);
            }
        }


        return PlatformResponse.successData(graph);
    }

    private List<String> getIpList(Integer systemId, String sourceType) {
        List<GlscSystemDeploy> systemDeploys = glscSystemDeployService.listBySystemId(systemId, sourceType);
        if (CollectionUtils.isNotEmpty(systemDeploys)) {
            List<String> ipList = Lists.newArrayList();
            systemDeploys.forEach(e -> ipList.add(e.getNodeIp()));
            return ipList;
        }
        return Lists.newArrayList();
    }


    /**
     * 根据组件id生成组件拓扑图
     *
     * @param id
     * @return
     */
    @GetMapping("/part/graph")
    public PlatformResponse<TopologicalGraphDto> partGraph(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        TopologicalGraphDto graph = initGraph();

        GlscSystemResource system = glscSystemResService.findById(id);
        TopologicalGraphNodeDto mainNode = TopologicalGraphNodeDto.builder().build();
        mainNode.setId("z" + String.valueOf(system.getId()));
        mainNode.setName(system.getName());
        mainNode.setSymbolSize(60);
        mainNode.setCategory(1);
        mainNode.setIps(getIpList(system.getId(), "2"));

        List<TopologicalGraphCategoryDto> categories = graph.getCategories();
        categories.add(TopologicalGraphCategoryDto.builder().name("关联系统").build());
//        categories.add(TopologicalGraphCategoryDto.builder().name("接入组件").build());
        categories.add(TopologicalGraphCategoryDto.builder().name("本系统").build());


        List<TopologicalGraphNodeDto> nodes = graph.getNodes();
        List<TopologicalGraphLinkDto> links = graph.getLinks();
        nodes.add(mainNode);
        Integer connectCount = 0;


        //被接入系统
        //{"pageNo":1,"pageSize":10,"systemId":"38","partnerType":"out_system","sourceType":"2"}
        GlscSystemPartnerDto beiJieRuDto = GlscSystemPartnerDto.builder().systemId(id).partnerType("out_system").sourceType("2").build();
        Page<GlscSystemPartner> beiJieRuPage = glscSystemPartnerService.page(beiJieRuDto);
        if (CollectionUtils.isNotEmpty(beiJieRuPage.getContent())) {

            connectCount += beiJieRuPage.getContent().size();

            for (GlscSystemPartner partner : beiJieRuPage.getContent()) {
                TopologicalGraphNodeDto node = TopologicalGraphNodeDto.builder().build();
                TopologicalGraphLinkDto link = TopologicalGraphLinkDto.builder().source("z" + String.valueOf(partner.getSystemId())).target(String.valueOf(partner.getPartnerSystemId())).build();

                node.setId(String.valueOf(partner.getPartnerSystemId()));
                node.setName(partner.getPartnerName());
                node.setSymbolSize(30);
                node.setCategory(0);
                node.setIps(getIpList(system.getId(), partner.getPartnerSourceType()));


                nodes.add(node);
                links.add(link);
            }
        }

        //接入组件
        //{"pageNo":1,"pageSize":10,"systemId":"38","partnerType":"in_middleware","sourceType":"2"}
        /*GlscSystemPartnerDto zuJianDto = GlscSystemPartnerDto.builder().systemId(id).partnerType("in_middleware").sourceType("2").build();
        Page<GlscSystemPartner> zuJianPage = glscSystemPartnerService.page(zuJianDto);
        if (CollectionUtils.isNotEmpty(zuJianPage.getContent())) {

            connectCount += zuJianPage.getContent().size();

            for (GlscSystemPartner partner : zuJianPage.getContent()) {
                TopologicalGraphNodeDto node = TopologicalGraphNodeDto.builder().build();
                TopologicalGraphLinkDto link = TopologicalGraphLinkDto.builder().source("z" + String.valueOf(partner.getSystemId())).target("z" + String.valueOf(partner.getPartnerSystemId())).build();

                node.setId("z" + String.valueOf(partner.getPartnerSystemId()));
                node.setName(partner.getPartnerName());
                node.setSymbolSize(20);
                node.setCategory(1);
                node.setIps(getIpList(system.getId(), partner.getPartnerSourceType()));

                nodes.add(node);
                links.add(link);
            }
        }*/

        return PlatformResponse.successData(graph);
    }

    /**
     * 初始化图标信息
     *
     * @return
     */
    public TopologicalGraphDto initGraph() {
        TopologicalGraphDto graph = new TopologicalGraphDto();
        graph.setCategories(Lists.newArrayList());
        graph.setNodes(Lists.newArrayList());
        graph.setLinks(Lists.newArrayList());
        return graph;
    }


}
